package game;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ImageIcon;

import java.awt.Font;

public class GameClient extends JFrame implements Runnable{

	Socket socket = null;
	BufferedWriter send = null;
	BufferedReader receive =null;
	boolean threadFlag = false;
	int win=0;
	int lose=0;
	int draw=0;
	ClientCount cc;
	
	private JPanel contentPane;
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JButton btnConnect;
	private JButton btnDisconnect;
	private JPanel panel_3;
	private JTextField serverIP;
	private JTextField textField_1;
	private JButton btnBawi;
	private JButton btnGawi;
	private JButton btnBo;
	private JPanel panel_5;
	private JPanel panel_6;
	private JPanel panel_7;
	private JPanel panel_8;
	private JLabel myFace;
	private JLabel youFace;
	private JPanel panel_10;
	private JLabel score;
	private JPanel panel_9;
	private JPanel panel_11;
	private JPanel panel_12;
	private JLabel myChoice;
	private JLabel youChoice;
	private JLabel label_2;
	private JLabel lblNewLabel_5;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_4;
	JLabel count;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GameClient frame = new GameClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GameClient() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 728, 512);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getPanel_1(), BorderLayout.SOUTH);
		contentPane.add(getPanel_5(), BorderLayout.CENTER);
		int i = (int)(Math.random()*10)+1;
		int j = (int)(Math.random()*10)+1;
		myChoice.setText("");
		youChoice.setText("");
		myChoice.setIcon(new ImageIcon("image\\imo"+i+".jpg"));
		youChoice.setIcon(new ImageIcon("image\\imo"+j+".jpg"));
		
		try {
			InetAddress ia = InetAddress.getLocalHost();
			setTitle("클라이언트 IP:"+ia.getHostAddress()+"/ port : 4444");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try{
		socket = new Socket(serverIP.getText(),4444);
		
		OutputStream os = socket.getOutputStream();
		OutputStreamWriter osw= new OutputStreamWriter(os);
		send = new BufferedWriter(osw);
		
		InputStream is = socket.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		receive = new BufferedReader(isr);
		
		threadFlag = true;

		sendMsg("bin");
		
			while (threadFlag) {
				String str = receive.readLine();
				
				if("cho".equals(str)){
					youFace.setIcon(new ImageIcon("image\\cho.jpg"));
				}else if("serverStop".equals(str)){
					threadFlag = false;
					cc.stop();
					count.setText("");
					btnGawi.setEnabled(false);
					btnBawi.setEnabled(false);
					btnBo.setEnabled(false);
					win=0;lose=0;draw=0;
					int i = (int)(Math.random()*10)+1;
					int j = (int)(Math.random()*10)+1;
					myChoice.setText("");
					youChoice.setText("");
					myChoice.setIcon(new ImageIcon("image\\imo"+i+".jpg"));
					youChoice.setIcon(new ImageIcon("image\\imo"+j+".jpg"));
					youFace.setIcon(new ImageIcon(""));
				}else if("Start".equals(str)){
					cc = new ClientCount(this);
					cc.start();
					btnGawi.setEnabled(true);
					btnBawi.setEnabled(true);
					btnBo.setEnabled(true);
					myChoice.setText("");
					youChoice.setText("");
					int i = (int)(Math.random()*10)+1;
					int j = (int)(Math.random()*10)+1;
					myChoice.setIcon(new ImageIcon("image\\imo"+i+".jpg"));
					youChoice.setIcon(new ImageIcon("image\\imo"+j+".jpg"));
					
				} else {
						
					if ("gaga".equals(str)) {
						myChoice.setIcon(new ImageIcon("image\\ga.jpg"));
						youChoice.setIcon(new ImageIcon("image\\ga.jpg"));
						draw++;
						
					} else if ("gaba".equals(str)) {
						myChoice.setIcon(new ImageIcon("image\\ga.jpg"));
						youChoice.setIcon(new ImageIcon("image\\ba.jpg"));
						lose++;
						
					} else if ("gabo".equals(str)) {
						myChoice.setIcon(new ImageIcon("image\\ga.jpg"));
						youChoice.setIcon(new ImageIcon("image\\bo.jpg"));
						win++;
						
					} else if ("baga".equals(str)) {
						myChoice.setIcon(new ImageIcon("image\\ba.jpg"));
						youChoice.setIcon(new ImageIcon("image\\ga.jpg"));
						win++;
						
					} else if ("baba".equals(str)) {
						myChoice.setIcon(new ImageIcon("image\\ba.jpg"));
						youChoice.setIcon(new ImageIcon("image\\ba.jpg"));
						draw++;

					} else if ("babo".equals(str)) {	
						myChoice.setIcon(new ImageIcon("image\\ba.jpg"));
						youChoice.setIcon(new ImageIcon("image\\bo.jpg"));
						lose++;

					} else if ("boga".equals(str)) {
						myChoice.setIcon(new ImageIcon("image\\bo.jpg"));
						youChoice.setIcon(new ImageIcon("image\\ga.jpg"));
						lose++;

					} else if ("boba".equals(str)) {
						myChoice.setIcon(new ImageIcon("image\\bo.jpg"));
						youChoice.setIcon(new ImageIcon("image\\ba.jpg"));
						win++;

					} else if ("bobo".equals(str)) {
						myChoice.setIcon(new ImageIcon("image\\bo.jpg"));
						youChoice.setIcon(new ImageIcon("image\\bo.jpg"));
						draw++;

					} else if ("aaaa".equals(str)) {
						myChoice.setIcon(new ImageIcon(""));
						youChoice.setIcon(new ImageIcon(""));
						myChoice.setText("시간");
						youChoice.setText("초과");;
						draw++;
						
					} else if ("bbbb".equals(str)) {
						myChoice.setIcon(new ImageIcon(""));
						youChoice.setIcon(new ImageIcon(""));
						myChoice.setText("승");
						youChoice.setText("리");
						win++;
						
					} else if ("cccc".equals(str)) {
						myChoice.setIcon(new ImageIcon(""));
						youChoice.setIcon(new ImageIcon(""));
						myChoice.setText("패");
						youChoice.setText("배");
						lose++;
						
					}
					cc.stop();
					count.setText("");
					score.setText("전적 승:" + win + " 패:" + lose + " 무:" + draw);
					btnGawi.setEnabled(false);
					btnBawi.setEnabled(false);
					btnBo.setEnabled(false);
				}
				
			}
		
		receive.close(); isr.close(); is.close();
		send.close(); osw.close(); os.close();
		socket.close();
		btnDisconnect.setEnabled(false);
		btnConnect.setEnabled(true);
		
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public void serverConnect(){
		try {
			Thread t = new Thread(this);
			t.start();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public void serverDisconnect(){
		try {
			send.write("Disconnect");
			send.newLine();
			send.flush();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		threadFlag =false;
		btnConnect.setEnabled(true);
		btnDisconnect.setEnabled(false);
		btnGawi.setEnabled(false);
		btnBawi.setEnabled(false);
		btnBo.setEnabled(false);
	}

	public void sendMsg(String ms){
		try{
			send.write(ms);
			send.newLine();
			send.flush();
			
		} catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getPanel_2(), BorderLayout.EAST);
			panel.add(getPanel_3(), BorderLayout.CENTER);
			panel.add(getCount(), BorderLayout.WEST);
		}
		return panel;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new GridLayout(0, 3, 0, 0));
			panel_1.add(getBtnGawi());
			panel_1.add(getBtnBawi());
			panel_1.add(getBtnBo());
		}
		return panel_1;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.add(getBtnConnect());
			panel_2.add(getBtnDisconnect());
		}
		return panel_2;
	}
	private JButton getBtnConnect() {
		if (btnConnect == null) {
			btnConnect = new JButton("접속");
			btnConnect.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					serverConnect();
					btnConnect.setEnabled(false);
					btnDisconnect.setEnabled(true);
				}
			});
		}
		return btnConnect;
	}
	private JButton getBtnDisconnect() {
		if (btnDisconnect == null) {
			btnDisconnect = new JButton("해제");
			btnDisconnect.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverDisconnect();
					btnConnect.setEnabled(true);
					btnDisconnect.setEnabled(false);
					youFace.setIcon(new ImageIcon(""));
				}
			});
			btnDisconnect.setEnabled(false);
		}
		return btnDisconnect;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setLayout(new GridLayout(0, 2, 0, 0));
			panel_3.add(getServerIP());
			panel_3.add(getTextField_1());
		}
		return panel_3;
	}
	private JTextField getServerIP() {
		if (serverIP == null) {
			serverIP = new JTextField();
			serverIP.setText("192.168.10.122");
			serverIP.setColumns(10);
		}
		return serverIP;
	}
	private JTextField getTextField_1() {
		if (textField_1 == null) {
			textField_1 = new JTextField();
			textField_1.setColumns(10);
		}
		return textField_1;
	}
	private JButton getBtnBawi() {
		if (btnBawi == null) {
			btnBawi = new JButton("바위");
			btnBawi.setEnabled(false);
			btnBawi.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					sendMsg("ba");
					myChoice.setIcon(new ImageIcon("image\\ba.jpg"));
					btnGawi.setEnabled(false);
					btnBawi.setEnabled(false);
					btnBo.setEnabled(false);
				}
			});
		}
		return btnBawi;
	}
	private JButton getBtnGawi() {
		if (btnGawi == null) {
			btnGawi = new JButton("가위");
			btnGawi.setEnabled(false);
			btnGawi.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					sendMsg("ga");
					myChoice.setIcon(new ImageIcon("image\\ga.jpg"));
					btnGawi.setEnabled(false);
					btnBawi.setEnabled(false);
					btnBo.setEnabled(false);
				}
			});
		}
		return btnGawi;
	}
	private JButton getBtnBo() {
		if (btnBo == null) {
			btnBo = new JButton("보");
			btnBo.setEnabled(false);
			btnBo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					sendMsg("bo");
					myChoice.setIcon(new ImageIcon("image\\ba.jpg"));
					btnGawi.setEnabled(false);
					btnBawi.setEnabled(false);
					btnBo.setEnabled(false);
				}
			});
		}
		return btnBo;
	}
	private JPanel getPanel_5() {
		if (panel_5 == null) {
			panel_5 = new JPanel();
			panel_5.setLayout(new BorderLayout(0, 0));
			panel_5.add(getPanel_6(), BorderLayout.WEST);
			panel_5.add(getPanel_10(), BorderLayout.CENTER);
		}
		return panel_5;
	}
	private JPanel getPanel_6() {
		if (panel_6 == null) {
			panel_6 = new JPanel();
			panel_6.setLayout(new GridLayout(0, 1, 0, 0));
			panel_6.add(getPanel_7());
			panel_6.add(getPanel_8());
		}
		return panel_6;
	}
	private JPanel getPanel_7() {
		if (panel_7 == null) {
			panel_7 = new JPanel();
			panel_7.setLayout(new BorderLayout(0, 0));
			panel_7.add(getMyFace(), BorderLayout.CENTER);
			panel_7.add(getLblNewLabel_2(), BorderLayout.NORTH);
		}
		return panel_7;
	}
	private JPanel getPanel_8() {
		if (panel_8 == null) {
			panel_8 = new JPanel();
			panel_8.setLayout(new BorderLayout(0, 0));
			panel_8.add(getYouFace(), BorderLayout.CENTER);
			panel_8.add(getLblNewLabel_4(), BorderLayout.NORTH);
		}
		return panel_8;
	}
	private JLabel getMyFace() {
		if (myFace == null) {
			myFace = new JLabel("");
			myFace.setHorizontalAlignment(SwingConstants.CENTER);
			myFace.setIcon(new ImageIcon("image\\bin.jpg"));
		}
		return myFace;
	}
	private JLabel getYouFace() {
		if (youFace == null) {
			youFace = new JLabel("");
			youFace.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return youFace;
	}
	private JPanel getPanel_10() {
		if (panel_10 == null) {
			panel_10 = new JPanel();
			panel_10.setLayout(new BorderLayout(0, 0));
			panel_10.add(getScore(), BorderLayout.NORTH);
			panel_10.add(getPanel_9_1(), BorderLayout.CENTER);
		}
		return panel_10;
	}
	private JLabel getScore() {
		if (score == null) {
			score = new JLabel("전적");
			score.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return score;
	}
	private JPanel getPanel_9_1() {
		if (panel_9 == null) {
			panel_9 = new JPanel();
			panel_9.setLayout(new GridLayout(0, 2, 0, 0));
			panel_9.add(getPanel_11());
			panel_9.add(getPanel_12());
		}
		return panel_9;
	}
	private JPanel getPanel_11() {
		if (panel_11 == null) {
			panel_11 = new JPanel();
			panel_11.setLayout(new BorderLayout(0, 0));
			panel_11.add(getLabel_1_1(), BorderLayout.CENTER);
			panel_11.add(getLabel_2(), BorderLayout.NORTH);
		}
		return panel_11;
	}
	private JPanel getPanel_12() {
		if (panel_12 == null) {
			panel_12 = new JPanel();
			panel_12.setLayout(new BorderLayout(0, 0));
			panel_12.add(getLabel_1_2(), BorderLayout.CENTER);
			panel_12.add(getLblNewLabel_5(), BorderLayout.NORTH);
		}
		return panel_12;
	}
	private JLabel getLabel_1_1() {
		if (myChoice == null) {
			myChoice = new JLabel("");
			myChoice.setFont(new Font("궁서", Font.PLAIN, 55));
			myChoice.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return myChoice;
	}
	private JLabel getLabel_1_2() {
		if (youChoice == null) {
			youChoice = new JLabel("");
			youChoice.setFont(new Font("궁서", Font.PLAIN, 55));
			youChoice.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return youChoice;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("나의 선택");
			label_2.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return label_2;
	}
	private JLabel getLblNewLabel_5() {
		if (lblNewLabel_5 == null) {
			lblNewLabel_5 = new JLabel("상대방의 선택");
			lblNewLabel_5.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNewLabel_5;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("\uB0B4 \uC774\uBBF8\uC9C0");
		}
		return lblNewLabel_2;
	}
	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("\uC0C1\uB300\uBC29 \uC774\uBBF8\uC9C0");
		}
		return lblNewLabel_4;
	}
	private JLabel getCount() {
		if (count == null) {
			count = new JLabel("");
		}
		return count;
	}
}
